﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using System.Data;
using System.Data.SqlClient;

using WpfBitsLib;

namespace WpfBitsControls
{
    /// <summary>
    /// Interaction logic for Statistics.xaml
    /// </summary>
    public partial class Statistics : UserControl, IPage
    {
        private bool hasLoaded =false;
        private int level = 0;
        string AppFormNum = "";
        string Course = "";
        string LastCourse = "";
        string Exam = "";
        string Mark = "";
        string Dated = "";
        string LastExam = "";

        DataTable dt = null;

        string Subject = "";
        string LastSubject = "";
        TreeViewItem tvi = null;
        TreeViewItem Level1Item = null;
        TreeViewItem Level2Item = null;
        
        public Statistics()
        {
            InitializeComponent();
            
         }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (hasLoaded) return;
            LoadTree();
           
            hasLoaded = true;
        }

        public void LoadTree()
        {
            
            dt = (DataTable)((IHelper)this.DataContext).Stuff;
            level = -1;

            ProcessRow(0);

        }

        private int ProcessRow(int RowNo)
        {
            for (int r = RowNo; r <= dt.Rows.Count - 1; r++)
            {
                AppFormNum = dt.Rows[r]["intAppForm"].ToString();
                Course = dt.Rows[r]["strCourseCode"].ToString();
                Subject = dt.Rows[r]["strSubjectCode"].ToString();
                Exam = dt.Rows[r]["strExam"].ToString();
                Mark = dt.Rows[r]["intMark"].ToString();
                Dated = dt.Rows[r]["dtmRecieved"].ToString();

               
                    if (Course != LastCourse)
                    {
                        level = 0;
                    }
                    else if (Subject != LastSubject)
                    {
                        level = 1;
                    }
                    else if (Subject == LastSubject)
                    {
                        level = 2;   
                    }
                    
                    if (level <= 0) //Course
                    {
                        tvi = new TreeViewItem();
                        tvi.Header = AppFormNum + " (" + Course + ") : " + dt.Rows[r]["strEngDescriptionC"].ToString();
                        tvi.Tag = "course";
                        treExams.Items.Add(tvi);
                        Level1Item = tvi;
                    }
                    if (level <= 1) //Subject
                    {
                        tvi = new TreeViewItem();
                        tvi.Header = Subject + " (" + dt.Rows[r]["strEngDescriptionS"].ToString() + ")";
                        tvi.Tag = "subject";
                        Level1Item.Items.Add(tvi);
                        Level2Item = tvi;
                    }
                    if (level <= 2) //Exam
                    {
                        tvi = new TreeViewItem();
                        tvi.Header = Exam;
                        tvi.Tag = "exam~"+Mark+"~"+Dated;
                        Level2Item.Items.Add(tvi);
                    }

                    LastCourse = Course;
                    LastSubject = Subject;
                    LastExam = Exam;

            }

            return RowNo;

        }

        private void treExams_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            TreeViewItem tvi = (TreeViewItem)e.NewValue;
            string[] r;
            char[] delimiterChars = {'~'};


            if (tvi.Tag.ToString().StartsWith("exam"))
            {
                r = tvi.Tag.ToString().Split(delimiterChars, StringSplitOptions.None);
                txtDateReceived.Text = r[2].ToString();
                txtExamMark.Text = r[1].ToString();



            }
            else
            {
                txtDateReceived.Text = "";
                txtExamMark.Text = "";

            }
        }
        
    }
}
